home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / V_SCROLL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  13.9 KB  |  461 lines

  1. package sub_arctic.lib;
  2.  
  3.  
  4. import sub_arctic.input.*;
  5. import sub_arctic.output.*;
  6.  
  7. /**
  8.  * This is a "style-free" vertical scrollbar interactor. It works with
  9.  * the style_manager and a style object.
  10.  * 
  11.  * @author Ian Smith
  12.  */
  13.  
  14. public class v_scrollbar extends v_slider {
  15.  
  16.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  17.  
  18.   /** 
  19.    * Indicate which values (coordinates/sizes) of this object are
  20.    * intrinsically constrained by the internals of the object.  In this case
  21.    * we need to remove PART_B from the set provided by the superclass.
  22.    *
  23.    * @return int bitset indicating which parts are intrinsically constrained.
  24.    */
  25.   public int intrinsic_constraints()
  26.     {
  27.       return super.intrinsic_constraints() & ~ PART_B;
  28.     }
  29.  
  30.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  31.  
  32.   /**
  33.    * Compute the offset to the thumb in pixels.  We override here so we never
  34.    * return -1 for the doesn't fit condition (which was for use by sliders
  35.    * that have a fixed size thumb), and just return 0 instead.
  36.    *
  37.    * @return int the amount the thumb should be shifted down to account for the 
  38.    *         value of the slider.
  39.    */
  40.   protected int thumb_offset()
  41.     {
  42.       int result = super.thumb_offset();
  43.       if (result < 0) result = 0;
  44.       return result;
  45.     }
  46.  
  47.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  48.  
  49.   /**
  50.    * This holds the usable area of the scrollbar. 
  51.    */
  52.   protected int _usable_area;
  53.  
  54.   /**
  55.    * This computes the usable area of the scrollbar.
  56.    */
  57.   protected void compute_usable_area() {
  58.     loaded_image up=up_img();
  59.     loaded_image down=dn_img();
  60.  
  61.     /* sanity check */
  62.     if ((up==null) || (down==null)) {
  63.       _usable_area=0;
  64.     }
  65.  
  66.     /* normal case */
  67.     _usable_area=h()-(up.height()+down.height());
  68.   }
  69.  
  70.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  71.   
  72.   /** 
  73.    * Return the value of the part_b component of this object.  In our case 
  74.    * this is tied to _thumb_percentage.  In particular its 
  75.    * thumb_percentage*thumb_scale where thumb_percentage is in the range 
  76.    * 0..1. 
  77.    *
  78.    * @return int the value of part_b (AKA thumb_percentage).
  79.    */
  80.   public int part_b()
  81.     {
  82.       /* Make sure its up to date and then return it */
  83.       eval_part_b();
  84.       return (int)(((double)_thumb_percentage)*thumb_scale() + 0.5);
  85.     }
  86.  
  87.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  88.  
  89.   /** 
  90.    * Set part_b value directly bypassing the constraint system (but doing 
  91.    * damage).  In this case part_b is tied to _thumb_percentage.
  92.    *
  93.    * @param int v the new value.
  94.    */
  95.   protected void set_raw_part_b(int v) 
  96. {
  97.       double new_v;
  98.  
  99.       /* compute the floating point equivalent */
  100.       new_v = ((double)v)/thumb_scale();
  101.       if (new_v < 0.0) new_v = 0.0;
  102.       if (new_v > 1.0) new_v = 1.0;
  103.  
  104.       /* don't do anything unless this is a change */
  105.       if (new_v != _thumb_percentage)
  106.     {
  107.       /* make change */
  108.           _thumb_percentage = new_v;
  109.  
  110.           /* make sure we have the usable area and make a new image */
  111.           compute_usable_area();
  112.           calculate_thumb_image(); // this does damage_self();
  113.     }
  114.     }
  115.  
  116.    //had:
  117.    //* @exception general PROPAGATED
  118.  
  119.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  120.  
  121.   /** 
  122.    * Set the part_b component of this object.  In our case this is tied to 
  123.    * thumb_percentage. 
  124.    *
  125.    * @param int v the new value
  126.    */
  127.   public void set_part_b(int v) 
  128. {
  129.       double old_v;
  130.  
  131.       /* if this has a constraint throw an exception */
  132.       if ((active_constraints() & PART_B) != 0)
  133.         throw new sub_arctic_error(
  134.       "Attempt to assign value to constrained part_b " +
  135.       "(AKA v_scrollbar.thumb_percentage)");
  136.  
  137.       /* set the value */
  138.       old_v = _thumb_percentage;
  139.       set_raw_part_b(v);
  140.  
  141.       /* if its changed, mark it out of date */
  142.       if (old_v != _thumb_percentage) mark_part_b_ood();
  143.     }
  144.  
  145.    //had:
  146.    //* @exception cannot_assign if the value is constrained.
  147.    //* @exception PROPAGATED.
  148.  
  149.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  150.  
  151.   /** 
  152.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  153.    * return it (or accept it) as an integer through the part_b interface.  
  154.    * This defaults to 100.0 which corresponds to integer values being one
  155.    * percent each (i.e., 0..100 is the legal range). 
  156.    */
  157.   protected double _thumb_scale = 100.0;
  158.  
  159.   /** 
  160.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  161.    * return it (or accept it) as an integer through the part_b interface.  
  162.    * This defaults to 100.0 which corresponds to integer values being one
  163.    * percent each (i.e., 0..100 is the legal range). 
  164.    * 
  165.    * @return double the thumb scale value.
  166.    */
  167.   public double thumb_scale() {return _thumb_scale; }
  168.  
  169.   /** 
  170.    * The amount we scale thumb_percentage (which is in the range 0..1) by when
  171.    * return it (or accept it) as an integer through the part_b interface.  
  172.    * This defaults to 100.0 which corresponds to integer values being one
  173.    * percent each (i.e., 0..100 is the legal range).  If you want more 
  174.    * resolution than this, set the scale to a larger number.  Values <= 0
  175.    * will restore the scale value to the default (100.0). 
  176.    *
  177.    * @param double v the new thumb scale value.
  178.    */
  179.   public void set_thumb_scale(double v) 
  180.     {
  181.       if (v <= 0) v = 100.0; 
  182.       if (v != _thumb_scale)
  183.     {
  184.           _thumb_scale = v; 
  185.       mark_part_b_ood();
  186.     }
  187.      }
  188.  
  189.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  190.  
  191.   /**
  192.    * This is the amount as a percentage (0..1) of size the thumb takes up.
  193.    */
  194.   protected double _thumb_percentage;
  195.  
  196.   /**
  197.    * Retrieve the amount of space covered by the thumb.  This value is 
  198.    * tied to part_b of this object (which is accessible as a scaled integer
  199.    * via the constraint system).
  200.    *
  201.    * @return double the size of the scrollbar as a percent (0..1) of the area.
  202.    */
  203.   public double thumb_percentage() 
  204.     {
  205.       /* Make sure its up to date and then return it */
  206.       eval_part_b();
  207.       return _thumb_percentage;
  208.     };
  209.  
  210.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  211.  
  212.   /** Size of current thumb (so we know if we need to replace it) */
  213.   protected int _cur_thumb_size = -1;
  214.  
  215.   /**
  216.    * This does the work of actually drawing the thumb image.
  217.    */
  218.   public void calculate_thumb_image() {
  219.     int pixel_width;
  220.     int min=style_manager.current_style().v_scrollbar_minimum_thumb_size();
  221.  
  222.     /* what's the percentage */
  223.     pixel_width=Math.round((float)(((double)_usable_area)*
  224.                    thumb_percentage()));
  225.     /* is it too small? */
  226.     if (pixel_width<min) {
  227.       pixel_width=min;
  228.     }
  229.  
  230.     /* if we have changed, build the thumb image */
  231.     if (pixel_width != _cur_thumb_size)
  232.       {
  233.     set_thumb_img(style_manager.current_style().  
  234.       v_scrollbar_thumb(pixel_width));
  235.     _cur_thumb_size = pixel_width;
  236.       }
  237.   }
  238.  
  239.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  240.  
  241.   /**
  242.    * Set the percentage of the whole size the thumb is now occupying.
  243.    * 
  244.    * @param double the percentage of the size of the scrollbar the thumb is 
  245.    *               occupying (0.0 < this number <= 1.0)
  246.    */
  247.   public void set_thumb_percentage(double d) {
  248.  
  249.     /* force d in range */
  250.     if (d < 0.0) d = 0.0;
  251.     if (d > 1.0) d = 1.0;
  252.  
  253.     /* only do something if we have a change */
  254.     if (d==_thumb_percentage) return;
  255.  
  256.     /* do the assignment and mark part_b out of date as a result */
  257.     _thumb_percentage = d;
  258.     mark_part_b_ood();
  259.  
  260.     /* make sure we have the usable area and recompute image */
  261.     compute_usable_area();
  262.     calculate_thumb_image(); // this does damage_self();
  263.   }
  264.  
  265.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  266.  
  267.   /**
  268.    * Internal function for initializing the scrollbar.
  269.    */
  270.   protected void init() {
  271.  
  272.     /* this is the same as a change */
  273.     style_changed();
  274.  
  275.     /* set our intrinsic width to the width of any of the images */
  276.     /* sanity check first */
  277.     if (up_img()==null) {
  278.       set_intrinsic_w(10); // bogus but the user will want to see something
  279.     } else {
  280.       set_intrinsic_w(up_img().width());
  281.     }
  282.  
  283.     set_boxed(false); /* do we never want this? */
  284.   }
  285.  
  286.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  287.  
  288.   /**
  289.    * Create a vertical scrollbar at a given location on the screen with
  290.    * a given height.
  291.    * @param int             x       the x position of this object.
  292.    * @param int             y       the y position of this object.
  293.    * @param int             h       the height of this object.
  294.    * @param int             min     the min value of this object.
  295.    * @param int             max     the max value of this object.
  296.    * @param int             init    the initial value of this object.
  297.    * @param double          percent the size of the scrollbar (larger than 
  298.    *                                zero, less than or equal to one).
  299.    * @param int             small   the small increment value (which occurs 
  300.    *                                when you press a end button).
  301.    * @param int             large   the large increment value (which occurs 
  302.    *                                when you click in the thumb area).
  303.    * @param callback_object co      the object to send callbacks to (#0 is the 
  304.    *                                mouse has been released and the thumb is 
  305.    *                                stopped moving and #1 is the thumb is being 
  306.    *                                dragged).
  307.    */
  308.   public v_scrollbar(int x, int y, int h, int min, int max, int init,
  309.              double percent,int small, int large, 
  310.              callback_object co) {
  311.  
  312.     // temporarily construct a default vertical slider
  313.     super(x,y,h,min,max,init,small,large,co);
  314.  
  315.     /* put the percent in */
  316.     if (percent<0.0) {
  317.       _thumb_percentage=0.0;
  318.     } else if (percent>=1.0) {
  319.       _thumb_percentage=1.0;
  320.     } else {
  321.       _thumb_percentage=percent;
  322.     }
  323.  
  324.     init();
  325.   }
  326.  
  327.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  328.  
  329.   /**
  330.    * Create a vertical scrollbar with some reasonable defaults.
  331.    * This makes the range of the scrollbar 0-99 and sets the initial
  332.    * value at 0. It makes the small increment 5 and the large
  333.    * increment 25. It sets the thumb to be 25% of the size of the
  334.    * scrollbar
  335.    * 
  336.    * @param int             x  the x position of the interactor.
  337.    * @param int             y  the x position of the interactor.
  338.    * @param int             w  the width of the interactor.
  339.    * @param callback_object co the object to send callbacks to.
  340.    */
  341.   public v_scrollbar(int x, int y, int w, callback_object co ) {
  342.  
  343.     super(x,y,w,0,99,0,5,25,co);
  344.  
  345.     /* set the percentage */
  346.     _thumb_percentage=0.25;
  347.  
  348.     /* set things up */
  349.     init();
  350.   }
  351.  
  352.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  353.  
  354.   /**
  355.    * Minimal constructor for a vertical scrollbar. It assumes
  356.    * you are going to size it and position it with constraints 
  357.    * or do so directly.<p>
  358.    *
  359.    * This makes the range of the slider 0-99 and sets the initial
  360.    * value at 0. It makes the small increment 5 and the large
  361.    * increment 25. It sets the thumb to be 25% of the size of the
  362.    * scrollbar
  363.    * 
  364.    * @param callback_object co the object to send callbacks to
  365.    */
  366.   public v_scrollbar(callback_object co ) {
  367.  
  368.     super(0,0,50,0,99,0,5,25,co);
  369.  
  370.     /* set the percentage */
  371.     _thumb_percentage=0.25;
  372.  
  373.     /* set things up */
  374.     init();
  375.   }
  376.  
  377.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  378.  
  379.   /**
  380.    * This function gets called when the system's style resources
  381.    * change or we are getting initialized.
  382.    */
  383.   public void style_changed() {
  384.     style cs=style_manager.current_style();
  385.     loaded_image img[];
  386.  
  387.     /* make the images */
  388.     img=cs.v_scrollbar_images();
  389.  
  390.     /* set them into their places */
  391.     set_up_img(img[0]);
  392.     set_dn_img(img[1]);
  393.     set_back_img(img[2]);
  394.  
  395.     /* recalculate the thumb */
  396.     compute_usable_area();
  397.     calculate_thumb_image();
  398.  
  399.     /* put the shift on.. */
  400.     set_thumb_shift(cs.v_scrollbar_thumb_shift());
  401.   }
  402.  
  403.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  404.  
  405.   /**
  406.    * We override configure so we can automatically update the
  407.    * size of the scrollbar thumb.
  408.    */
  409.   public void configure() {
  410.     int pixel_height;
  411.     int min=style_manager.current_style().v_scrollbar_minimum_thumb_size();
  412.  
  413.     /* if things start breaking, this might need to go */
  414.     super.configure();
  415.  
  416.     /* make sure we have the usable area right */
  417.     compute_usable_area();
  418.  
  419.     /**
  420.      * WARNING: This code is basically the same as the code in 
  421.      * calculate_thumb_image() but it cannot call anything which
  422.      * would result in a call to damage_self() since this
  423.      * function is called in response to that.
  424.      */
  425.     /* what's the percentage */
  426.     pixel_height=Math.round((float)(((double)_usable_area)*
  427.                    thumb_percentage()));
  428.     /* is it too small? */
  429.     if (pixel_height<min) {
  430.       pixel_height=min;
  431.     }
  432.  
  433.     /* if we have a change, build the thumb image */
  434.     if (pixel_height != _cur_thumb_size)
  435.       {
  436.     _thumb_img = style_manager.current_style().
  437.           v_scrollbar_thumb(pixel_height);
  438.     _cur_thumb_size = pixel_height;
  439.       }
  440.   }
  441.  
  442.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  443. }
  444.  
  445. /*=========================== COPYRIGHT NOTICE ===========================
  446.  
  447. This file is part of the subArctic user interface toolkit.
  448.  
  449. Copyright (c) 1996 Scott Hudson and Ian Smith
  450. All rights reserved.
  451.  
  452. The subArctic system is freely available for most uses under the terms
  453. and conditions described in 
  454.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  455. and appearing in full in the lib/interactor.java source file.
  456.  
  457. The current release and additional information about this software can be 
  458. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  459.  
  460. ========================================================================*/
  461.